auto.waitFor();
var storage = storages.create("xiangxiangyuedu");

var time = (2000, 3000)

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!");
    while (true) {
      var allow = text("允许").findOne(5000);
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!");
        allow.click();
        sleep(1000);
        thread.interrupt();
        break;
      }
    }
  });
  sleep(1000);
  var result = launchApp(app_name);
  sleep(3000);
  thread.interrupt();
  return result;
}

function 广告() {
  let try_count = 14, close_btn = null;
  while (!close_btn) {
    if (try_count <= 0) {
      break
    }
    // 直到找到关闭按钮
    close_btn = idContains("ad_close").findOne(1000) ||
      className('android.widget.ImageView').clickable().depth(5).drawingOrder(2).findOne(1000)
    if (close_btn) {
      close_btn.click()
      sleep(time)
      break
    } else {
      try {
        取消 = text("取消").findOne(500)
        if (取消) {
          点击控件(取消)
        }
      } catch (error) { }
      try {
        关闭 = text("关闭").findOne(500)
        if (关闭) {
          点击控件(关闭)
        }
      } catch (error) { }
      try {//支付宝
        if (currentPackage() != 'com.x7game') {
          toastLog('估计是进入支付宝页面了')
          sleep(2000)
          back()
          sleep(2000)
          继续 = textStartsWith("继续").findOne(1000)
          if (继续) {
            点击控件(继续)
          }
          var 每日任务 = text('每日任务').findOne(3000)
          if (每日任务) {
            break
          }
        }
      } catch (error) { }
    }
    try_count--
    sleep(2000)
  }
}

// 点击控件坐标，控件的clickbale为false的时候
function 点击控件(x) {
  var weight = x.bounds()
  click(weight.centerX(), weight.centerY())
}

function Toast() {
  var 我知道了 = text('我知道了').findOne(25000)
  if (我知道了) {
    sleep(time)
    点击控件(我知道了)
  }
}

function login() {
  var 登录 = text('登录').findOne(3000)
  if (登录) {
    toastLog('当前在登录界面')
    var 请输入您的账号 = text('请输入您的账号').findOne(3000)
    if (请输入您的账号) {
      sleep(time)
      点击控件(请输入您的账号)
      sleep(time)
      if (device.model == 'Redmi 8A') {
        click(random(86, 640), random(547, 628))
      }
      if (device.model == 'MI 5X') {
        click(random(183, 962), random(851, 952))
      }
      sleep(time)
      click(436, 388)
      sleep(1000)
      click(420, 370)
      sleep(time)
      点击控件(登录)
      sleep(time)
    }
  } else {
    toastLog('您已在登录状态')
  }
}

function task() {
  var 开始 = text('开始').findOne(3000)
  if (开始) {
    sleep(time)
    点击控件(开始)
  }
  var 点击领取 = text('点击领取').findOne(1000)
  if (点击领取) {
    点击控件(点击领取)
    sleep(random(11000, 13000))
    sleep(time)
  }
  // var 每日任务 = text('每日任务').findOne(3000)
  // if (每日任务) {
  //   点击控件(每日任务)
  //   sleep(time)
  //   swip_up()
  //   sleep(time)
  //   var index = 0
  //   while (index < 35) {
  //     var 去完成 = text('去完成').findOne(5000)
  //     if (去完成) {
  //       点击控件(去完成)
  //       sleep(5000)
  //       if (currentActivity() != 'com.x7game.MainActivity') {
  //         toastLog('当前是广告页面')
  //         广告()
  //       } else {
  //         toastLog('没进去广告页面')
  //         sleep(time)
  //       }
  //       sleep(time)
  //     } else {
  //       toastLog('视频观看结束')
  //       index = 40
  //     }
  //     index++
  //   }
  // }
}

function main() {
  var open_app = launchAppEnhance("好玩吧")
  if (open_app) {
    Toast()
    login()
    task()
  }
  else {
    toastLog("好玩吧打开失败，请确认是否安装并授权打开!");
  }
}

try {
  main();
} catch (error) {
  toastLog(error);
}

// 随机向上滑动
function swip_up() {
  var w = device.width
  var h = device.height

  function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = { "x": 0, "y": 0 };
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
  };


  function sml_move(qx, qy, zx, zy, time) {
    //仿真随机带曲线滑动: qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
    var xxy = [time];
    var point = [
      { "x": qx, "y": qy },
      { "x": random(qx - 100, qx + 100), "y": random(qy, qy + 50) },
      { "x": random(zx - 100, zx + 100), "y": random(zy, zy + 50) },
      { "x": zx, "y": zy }
    ];
    for (let i = 0; i < 1; i += 0.08) {
      xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]
      xxy.push(xxyy);
    }
    gesture.apply(null, xxy);
  }

  sml_move(w / 2, h * 0.8 + h * random() * 0.1, w / 2, h * 0.15, 200 + 600 * random());
}

storage.put("script_running_status", "end");